package com.zxy.ziems.common.pojo.collector;

import lombok.Builder;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 *
 * @author 10348359
 * @create 2024-04-17 17:31
 */
@Data
public class Unit {
    /**
     * 主键
     */
    private String id;

    /**
     * 倍率 默认为1
     */
    private Integer multiple;

    /**
     * 除数 默认为1
     */
    private Integer divisor;

    /**
     * 单位换算
     * @param value
     * @param from
     * @return
     */
    public BigDecimal calculateFromUnit(BigDecimal value, Unit from) {
        return value.multiply(BigDecimal.valueOf(from.getMultiple()))
                .divide(BigDecimal.valueOf(from.getDivisor()), RoundingMode.HALF_UP)
                .multiply(BigDecimal.valueOf(this.getMultiple()))
                .divide(BigDecimal.valueOf(this.getDivisor()), RoundingMode.HALF_UP);
    }

    public BigDecimal calculateToUnit(BigDecimal value, Unit to) {
        return to.calculateFromUnit(value, this);
    }
}
